using System;
using System.Globalization;
using System.IO;
using System.Threading;
using System.Web;
using Sedna.Core.Security;

namespace Sedna.Core.Globalization
{
	public class HelpManager : IHelpManager
	{
		private string basePath = "~/help/";
		private IPrincipalWrapper principalWrapper;

		public string BasePath
		{
			get { return basePath; }
			set { basePath = value; }
		}

		public IPrincipalWrapper PrincipalWrapper
		{
			get { return principalWrapper; }
			set { principalWrapper = value; }
		}

		public string RelateHelpUrl(string relativePath)
		{
			return RelateHelpUrl(relativePath, principalWrapper.Culture);
		}

		public string RelateHelpUrl(string relativePath, CultureInfo cultureInfo)
		{
			string currentBasePath = basePath;
			if(cultureInfo.IsNeutralCulture)
			{
				currentBasePath += "neutral/";
			}
			else
			{
				currentBasePath += cultureInfo.ToString() + "/";
			}

			string localPath = HttpContext.Current.Request.MapPath(currentBasePath + relativePath);
			if(File.Exists(localPath))
			{
				return currentBasePath + relativePath;
			}
			return basePath + "neutral/" + relativePath;
		}
	}
}
